package com.zxl.live.screen.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zxl.live.screen.R;
import com.zxl.live.screen.screen.ui.fragment.ScreenSettingFragment;
import com.zxl.live.screen.screen.ui.fragment.SuiScreenFragment;
import com.zxl.live.screen.screen.ui.helper.data.SuiScreenDataHelper;
import com.zxl.live.screen.ui.widget.ViewPagerFixed;
import com.zxl.live.tools.base.BaseAppCompatFragment;

/**
 * Created by Administrator on 2018/2/11.
 */

public class ScreenFragment extends BaseAppCompatFragment {

    private ViewPagerFixed viewPagerFixed;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_screen, container, false);
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPagerFixed = (ViewPagerFixed) view.findViewById(R.id.view_pager);
        viewPagerFixed.setAdapter(new ScreenAdapter(getAppCompatActivity().getSupportFragmentManager()));
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPagerFixed);
        int position = getAppCompatActivity().getIntent().getIntExtra("position", 0);
        viewPagerFixed.setCurrentItem(position);
    }

    public int getCurrentTab() {
        if (null != viewPagerFixed) {
            return viewPagerFixed.getCurrentItem();
        }
        return 0;
    }

    class ScreenAdapter extends FragmentStatePagerAdapter {

        public ScreenAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            try {
                ScreenData screenData = ScreenData.values()[position];
                Fragment fragment = (Fragment) screenData.cls.newInstance();
                if (SuiScreenFragment.class.isInstance(fragment)) {
                    SuiScreenFragment tmp = (SuiScreenFragment) fragment;
                    tmp.setDataType(screenData.type);
                }
                return fragment;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return getResources().getString(ScreenData.values()[position].title);
        }

        @Override
        public int getCount() {
            return ScreenData.values().length;
        }
    }


    public enum ScreenData {

        SCREEN(R.string.screen_title, ScreenSettingFragment.class, null),

        SUI_SCREEN(R.string.sui_screen_title, SuiScreenFragment.class, SuiScreenDataHelper.DataType.SUI),

        ANIMATION_SCREEN(R.string.anim_screen_title, SuiScreenFragment.class, SuiScreenDataHelper.DataType.ANIMATION),

        KONG_BU_SCREEN(R.string.kuongbu_screen_title, SuiScreenFragment.class, SuiScreenDataHelper.DataType.KUONGBU),

        SYSTEM_ERROR_SCREEN(R.string.sys_screen_title, SuiScreenFragment.class, SuiScreenDataHelper.DataType.SYSTEM_ERROR);

        private int title;

        private Class cls;

        private SuiScreenDataHelper.DataType type;

        ScreenData(int title, Class cls, SuiScreenDataHelper.DataType type) {
            this.title = title;
            this.cls = cls;
            this.type = type;
        }

        public SuiScreenDataHelper.DataType getType() {
            return type;
        }
    }

}
